package cn.zhaoyuening.hadoopdemo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.CompressionCodecFactory;
import org.apache.hadoop.io.compress.CompressionInputStream;

import java.io.File;
import java.io.IOException;
import java.net.URI;

public class FileCompression {
    public static void main(String[] args) throws IOException {
        Configuration conf = new Configuration();
        String filePathStr = args[0];
        FileSystem fileSystem = FileSystem.get(URI.create(filePathStr),conf);

        Path filePath = new Path(filePathStr);
        CompressionCodecFactory codecFactory = new CompressionCodecFactory(conf);
        CompressionCodec codec = codecFactory.getCodec(filePath);

        String outPathStr = CompressionCodecFactory.removeSuffix(filePathStr, codec.getDefaultExtension());
        Path outPath = new Path(outPathStr);

        CompressionInputStream inputStream = null;
        FSDataOutputStream outputStream = null;


        try {
            outputStream = fileSystem.create(outPath);
            inputStream = codec.createInputStream(fileSystem.open(filePath));
            IOUtils.copyBytes(inputStream, outputStream, 4096, false);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            inputStream.close();
            outputStream.close();
        }

    }
}
